/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package android.reasons;

import android.app.ContextoAndroid;
import android.app.FontManager;
import android.app.Mensaje;
import android.app.ProgressDialog;
import android.entidades.Usuario;
import android.os.AsyncTask;
import android.view.View;
import android.view.View.OnClickListener;
import android.web.service.WebService;
import android.widget.LinearLayout;
import android.widget.EditText;
import android.widget.ImageButton;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;

/**
 *
 * @author efraingutierrez
 */
public class LogIn implements OnClickListener 
{
    private Inicio inicio;
    private ImageButton accept,btnAtras;
    private LinearLayout abs;
    private EditText etID,etPassword;
    
    public LogIn(Inicio inicio)
    {
        this.inicio = inicio;
        inicio.setContentView(R.layout.login);
        abs = (LinearLayout)inicio.findViewById(R.id.absLi);
        abs.setBackgroundResource(R.drawable.turquesa);
        accept = (ImageButton)inicio.findViewById(R.id.btnOk);
        btnAtras = (ImageButton)inicio.findViewById(R.id.btnAtras);
        etID = (EditText)inicio.findViewById(R.id.etID);
        etPassword = (EditText)inicio.findViewById(R.id.etPass);
        accept.setOnClickListener(this);
        btnAtras.setOnClickListener(this);
        FontManager.changeFont(abs);
    }

    public void onClick(View arg0) 
    {
        if(arg0.getId() == R.id.btnOk)
        {
            final String nickStr = etID.getText().toString();
            final String passStr = etPassword.getText().toString();

            if(!nickStr.equals("") && !passStr.toString().equals(""))
            {

                if(WebService.conexionInternert())
                    new WebPetition().execute(nickStr,passStr);
                else Mensaje.Mostrar("Error: No Existe Conexion a Internet");
            }
            else Mensaje.Mostrar("Error: Datos Incompletos");
        }
        else
        {
            inicio.back();
        }
    }
    private class WebPetition extends AsyncTask<String, Integer, Boolean> {

        ProgressDialog dialog;
        @Override
        protected void onPreExecute() {
            dialog = new ProgressDialog(ContextoAndroid.context);
            dialog.setIndeterminate(false);
            dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            dialog.setCancelable(true);
            dialog.setMessage("Validando Usuario...");
            dialog.show();
        }
        @Override
        protected Boolean doInBackground(String... params) 
        {
            try {
                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("id",params[0]));
                nameValuePairs.add(new BasicNameValuePair("password",params[1]));
                this.publishProgress(10);
                JSONArray array = WebService.getJSONArray(nameValuePairs, "Valida", "Controladores/validarUsuario.php");
                this.publishProgress(40);
                String str = array.getString(0);
                this.publishProgress(100);
                if(str.equals("1"))
                {
                    nameValuePairs = new ArrayList<NameValuePair>();
                    nameValuePairs.add(new BasicNameValuePair("id",params[0]));
                    array = WebService.getJSONArray(nameValuePairs, "Usuario", "Controladores/getUsuario.php");
                    String correo = array.getString(0);
                    String pass = array.getString(1);
                    String nombre = array.getString(2);
                    String foto = array.getString(3);
                    int tipo = 0;
                    Usuario u = new Usuario(correo,pass,nombre,foto,tipo);
                    ContextoAndroid.usuario = u;
                    return true;
                }
                else return false;
            } catch (JSONException ex) {
                return false;
            }
        }

        @Override
        protected void onPostExecute(Boolean result) 
        {
            dialog.dismiss();
            if(result)
            {
                Mensaje.Mostrar("Ha Ingresado Satisfactoriamente");
                inicio.back();
            }
            else Mensaje.Mostrar("Error: Datos Incorrectos");
        }
        @Override
        protected void onProgressUpdate(Integer... values) 
        {
            dialog.setMessage("Cargando...");
            dialog.setProgress(values[0]);
        }
    }
}
